// pages/mine/info/name.js
const App = getApp()
import Util from '../../../utils/util'

Page({
  data: {
    init: false,

    user: {},
    name: '',
  },
  onLoad() {
    this.execLoad()
  },
  execLoad() {
    App.doPost({
      uri: '/app/user/info/profile/info',
      body: {},
      fnSucc: resp => {
        this.setData({
          init: true,
          user: resp.data,
          name: resp.data.name
        })
      }
    })
  },
  execSave() {
    if (!this.data.name) {
      Util.Notify.warn(`请填写昵称`)
      return
    }

    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/user/info/profile/name',
      body: {
        name: this.data.name
      },
      fnSucc: () => {
        wx.pub("ReloadUserInfo", {})
        Util.Toast.succ(`提交成功`)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false,
        })
      }
    })
  }
})